package com.shengruan.admin.action;

import com.shengruan.admin.jdbc.QuestionJdbc;
import com.shengruan.admin.vo.Question;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Constants;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author <a href="mailto:zhdqCN@gmail.com">DaQing Zhou</a>
 * @version 1.0
 * @CreationTime: 12-7-6 下午8:12
 * @Description: To change this template use File | Settings | File Templates.
 */
public class QuestionAction extends BaseAction {

    private QuestionJdbc questionJdbc = (QuestionJdbc)this.getJdbc(QuestionJdbc.class);


    /**
     * 添加商品
     * @throws Exception
     */
    public void add() throws Exception {
        String name = this.input("name");
        String content = this.input("content");

        Question question = new Question();
        question.setName(StringUtils.trim(name));
        question.setContent(StringUtils.trim(content));
        question.setCreateTime(System.currentTimeMillis() + "");

        questionJdbc.save(question);
        this.setRequest(Constants.MESSAGE, "添加问题成功。");
        this.redirect("/admin/article/question-list.ac");
    }

    public void getById() throws Exception {
        String id = this.input("id");
        Question question = questionJdbc.getById(Integer.valueOf(id));
        this.setRequest("question", question);

        this.forward("/admin/article/question-edit.jsp");
    }

    public void edit() throws Exception {
        String id = this.input("id");
        String name = this.input("name");
        String content = this.input("content");

         Question question = questionJdbc.getById(Integer.valueOf(id));
         question.setName(StringUtils.trim(name));
         question.setContent(StringUtils.trim(content));
         question.setCreateTime(System.currentTimeMillis() + "");

        questionJdbc.update(question);
        this.setRequest(Constants.MESSAGE, "修改问题成功。");
        this.redirect("/admin/article/question-list.ac");
    }


    public void list() throws Exception {
        List<Question> questionList = questionJdbc.loadAll();
        setRequest(Constants.QUESTION_LIST, questionList);
        this.forward("/admin/article/question-list.jsp");
    }

    public void faqs() throws Exception {
        List<Question> questionList = questionJdbc.loadAll();
        setRequest(Constants.QUESTION_LIST, questionList);
        this.forward("/faqs.html");
    }



    public void delete() throws Exception {
        String id = this.input("id");
        questionJdbc.deleteById(Integer.valueOf(id));
        this.setRequest(Constants.MESSAGE, "删除问题成功。");
        this.redirect("/admin/article/question-list.ac");
    }
}
